<?php

namespace App\Models\Family;

use App\Models\Family;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    protected $table = 'love_family_users';

    const UPDATED_AT = null;

    public function user()
    {
        return $this->belongsTo(\App\Models\User::Class);
    }

    public function family()
    {
        return $this->belongsTo(Family::Class);
    }

    public static function getFamilyUsers($id)
    {
        return static::where('family_id', $id)
            ->leftJoin('love_users as b', 'b.id', 'user_id')
            ->leftJoin('love_relation as c', 'c.id', 'relation_id')
            ->select(['love_family_users.*',
                'b.username','b.mobile',
                'c.name as relation_name'
            ])
            ->orderBy('love_family_users.is_householder', 'desc')
            ->get()->toArray();

        /*return  \App\Models\Family\User::where('family_id', function($query) use ($id){
                    $query->select('family_id')
                        ->from('love_family_users')
                        ->where('user_id', $id)->first();
                })
            ->leftJoin('love_users as b', 'b.id', 'user_id')
            ->leftJoin('love_relation as c', 'c.id', 'relation_id')
            ->get()->toArray();*/

    }


}
